﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
namespace JumbotCms.Extends.Vote
{
    public partial class _index : JumbotCms.API.FrontHtml
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckExtendState("Vote");
            Server.ScriptTimeout = 8;//脚本过期时间
            string ContentStr = LoadPlugin_Vote(Str2Str(q("id")));
            Response.Write(ContentStr);//直接输出
        }
        /// <summary>
        /// 生成链接页
        /// </summary>
        /// <returns></returns>
        private string LoadPlugin_Vote(string id)
        {
            string PageStr = string.Empty;
            PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "extends/vote/templates/index.htm");
            ReplaceSiteTags(ref PageStr); 
            doh.Reset();
            doh.SqlCmd = "SELECT [Id],[Title],[VoteText],[VoteNum],[VoteTotal],[Type],[Lock] FROM [jcms_extends_vote] WHERE [Id]=" + id;
            DataTable dtVote = doh.GetDataTable();
            if (dtVote.Rows.Count > 0)
            {
                for (int i = 0; i < dtVote.Columns.Count; i++)
                {
                    PageStr = PageStr.Replace("{$Vote" + dtVote.Columns[i].ColumnName + "}", dtVote.Rows[0][i].ToString());
                }
                PageStr = PageStr.Replace("{$VoteList}", p__getVote(id));

            }
            else
                return "参数错误,请不要修改参数提交";
            dtVote.Clear();
            dtVote.Dispose();
            return PageStr;
        }
        /// <summary>
        /// 显示调查信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private string p__getVote(string id)
        {
            string TempStr = "";
            TempStr = "";
            doh.Reset();
            doh.SqlCmd = "SELECT [Title],[VoteText],[VoteNum],[VoteTotal] FROM [jcms_extends_vote] WHERE [Id]=" + id + " And [Lock]=0";
            DataTable dtVote = doh.GetDataTable();
            if (dtVote.Rows.Count > 0)
            {
                string[] voteText = dtVote.Rows[0]["VoteText"].ToString().Split('|');
                string[] voteNum = dtVote.Rows[0]["VoteNum"].ToString().Split('|');
                int voteTotal = Convert.ToInt32(dtVote.Rows[0]["VoteTotal"].ToString());
                if (voteTotal > 0)
                {
                    for (int i = 0; i < voteText.Length; i++)
                    {
                        if (100 * Convert.ToInt32(voteNum[i]) / voteTotal > 50)
                            TempStr += "<dl><dt>" + (i + 1) + "、" + voteText[i] + "</dt><dd><span style=\"width:" + (100 * Convert.ToInt32(voteNum[i]) / voteTotal) + "%;\">(" + voteNum[i] + ")</span></dd></dl>";
                        else
                            TempStr += "<dl><dt>" + (i + 1) + "、" + voteText[i] + "</dt><dd><span style=\"width:" + (100 * Convert.ToInt32(voteNum[i]) / voteTotal) + "%;\"></span>(" + voteNum[i] + ")</dd></dl>";
                    }
                }
                else
                {
                    for (int i = 0; i < voteText.Length; i++)
                    {
                        TempStr += "<dl><dt>" + (i + 1) + "、" + voteText[i] + "</dt><dd><span style=\"width:0%;\"></span>(" + voteNum[i] + ")</dd></dl>";
                    }
                }
            }
            dtVote.Clear();
            dtVote.Dispose();
            return TempStr;

        }
    }
}
